package com.elinkway.infinitemovies.adapter;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.elinkway.infinitemovies.bean.LocalVideoEpisode;
import com.le123.ysdq.R;
import com.le123.ysdq.databinding.LayoutEpisodeLocalPlayItemBinding;

import java.util.ArrayList;

/**
 * Created by xiao on 21/4/17.
 */
public class EpisodeLocalPlayAdapter extends RecyclerView.Adapter<EpisodeLocalPlayAdapter.LocalEpisodeHolder>  {

    private Context mContext;
    private ArrayList<LocalVideoEpisode> episodes;
    private String mType;
    private LayoutInflater mLayoutInflater;

    private String mSelectProder = "";

    private OnItemClickListener mListener;

    public EpisodeLocalPlayAdapter(Context mContext, ArrayList<LocalVideoEpisode> episodes, String type) {
        this.mContext = mContext;
        this.episodes = episodes;
        this.mType = type;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    @Override
    public LocalEpisodeHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        return new LocalEpisodeHolder(mLayoutInflater.inflate(R.layout.layout_episode_local_play_item, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(LocalEpisodeHolder holder, int position) {
        if(holder instanceof LocalEpisodeHolder){
            ((LocalEpisodeHolder)holder).bind(episodes.get(position), position, mContext, mType, mSelectProder);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    public Object getItemByPosition(int position) {
        if (null != episodes) {
            return episodes.get(position);
        }
        return null;
    }

    public class LocalEpisodeHolder extends RecyclerView.ViewHolder {
        private LayoutEpisodeLocalPlayItemBinding mEpisodeBinding;

        public LocalEpisodeHolder(View itemView) {
            super(itemView);
            mEpisodeBinding = DataBindingUtil.bind(itemView);
        }

        public void bind(@NonNull LocalVideoEpisode episode, final int position, final Context context, final String type, String mSelectProder) {
            if("list".equals(type)){
                mEpisodeBinding.episodePlayItemNum.setText(episode.getName());
                mEpisodeBinding.episodePlayItemNum.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
            }else if("grid".equals(type)){
                mEpisodeBinding.episodePlayItemNum.setText(episode.getPorder());
                mEpisodeBinding.episodePlayItemNum.setGravity(Gravity.CENTER);
            }
            if (mSelectProder.equals(episode.getPorder())) {
                mEpisodeBinding.episodePlayItemNum.setTextColor(context.getResources().getColor(R.color.color_3599f8));
            } else {
                mEpisodeBinding.episodePlayItemNum.setTextColor(context.getResources().getColor(R.color.blue_and_white_selector));
            }
            mEpisodeBinding.episodePlayItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mListener != null){
                        mListener.onItemClick(v, position);
                    }
                }
            });
        }
    }

    public String getmSelectProder() {
        return mSelectProder;
    }

    public void setmSelectProder(String mSelectProder) {
        this.mSelectProder = mSelectProder;
    }

    public void setEpisodes(ArrayList<LocalVideoEpisode> episodes) {
        this.episodes=episodes;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return episodes.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mListener = onItemClickListener;
    }

    public interface OnItemClickListener {
        void onItemClick(View view, final int i);
    }
}

